<?php

namespace app\shop\controller\sys_admin;

use app\member\model\UsersLevelApplyModel;
use app\shop\model\PrizeWinModel;
use think\Db;

use app\AdminController;
use app\shop\model\GoodsModel;
use app\shop\model\GoodsSkuModel;
use app\shop\model\GoodsImgsModel;
use app\shop\model\GoodsPricesModel;
use app\shop\model\ShippingTplModel;
use app\shop\model\PrizeGoodsModel;

use app\mainadmin\model\PaymentModel;
use think\exception\Handle;


/**
 * 商品相关
 * Class Index
 * @package app\store\controller
 */
class PrizeGoods extends AdminController
{
    public $ext_status = 0;//额外默认指定商品状态
    public $is_supplyer = false;//是否后台供应商管理
    public $supplyer_id = 0;//当前操作的供应商ID
    public $_field = '';
    public $_pagesize = 0;
    //*------------------------------------------------------ */
    //-- 初始化
    /*------------------------------------------------------ */
    public function initialize()
    {
        parent::initialize();
        $this->Model = new PrizeGoodsModel();
        $this->assign('is_supplyer', $this->is_supplyer);
    }

    //*------------------------------------------------------ */
    //-- 首页
    /*------------------------------------------------------ */
    public function index()
    {
        $this->getList(true);
        return $this->fetch('index');
    }
    /*------------------------------------------------------ */
    //-- 获取列表 奖品列表
    //-- $runData boolean 是否返回模板
    /*------------------------------------------------------ */
    public function getList($runData = false, $is_delete = 0)
    {
        $this->assign('listType', $this->action);
        $runJson = input('runJson', 0, 'intval');

        $search['keyword'] = input('keyword', '', 'trim');
        if (empty($search['keyword']) == false) {
            $where['and'][] = "( goods_name like '%" . $search['keyword'] . "%')  OR ( goods_sn like '%" . $search['keyword'] . "%')";
        }

        $where[] = ['is_delete', '=', $is_delete];

        $data = $this->getPageList($this->Model, $where, $this->_field, $this->_pagesize);
        //dump($data);

        $this->assign("data", $data);
        $this->assign("search", $search);


        if ($runJson == 1) {
            return $this->success('', '', $data);
        } elseif ($runData == false) {
            $data['content'] = $this->fetch('list')->getContent();
            unset($data['list']);
            return $this->success('', '', $data);
        }
        return true;
    }

    /*------------------------------------------------------ */
    //-- 信息页调用
    //-- $data array 自动读取对应的数据
    /*------------------------------------------------------ */
    public function asInfo($data)
    {
        $this->Model = new PrizeGoods();


        $ShippingTplModel = new ShippingTplModel();
        if (settings('shipping_tmp_supplyer') == 0) {
            $this->assign("ShippingTpl", $ShippingTplModel->getRows());//运费模板
        } elseif ($this->is_supplyer == true) {
            $this->assign("ShippingTpl", $ShippingTplModel->getRows($data['supplyer_id']));//运费模板
        } else {
            $this->assign("ShippingTpl", $ShippingTplModel->getRows($this->supplyer_id));//运费模板
        }



        //$data['paylist']
        //dump(config('config.withdraw_account_type'));die;
        //dump($payTypeList);die;

        // $data['pay_type'] =explode(',',$data['pay_type']);

        //dump(strstr($data['pay_type'],'alipay'));die;
        return $data;
    }



    /*------------------------------------------------------ */
    //-- 添加前调用
    /*------------------------------------------------------ */
    public function beforeAdd($row)
    {
        $row['addtime'] = time();
        //$row = $this->checkData($row);
        Db::startTrans();//启动事务
        return $row;
    }
    /*------------------------------------------------------ */
    //-- 添加后调用
    /*------------------------------------------------------ */
    public function afterAdd($row)
    {

        $row['addtime'] = time();
        Db::commit();// 提交事务

        return $this->success('添加成功', url('index'));
    }
    /*------------------------------------------------------ */
    //-- 修改前处理
    /*------------------------------------------------------ */
    public function beforeEdit($row)
    {

        if($row['num']<0){
            return $this->error('奖品数量必须大于等于0', '');
        }

        $row['updatetime'] = time();

        Db::startTrans();//启动事务
        return $row;
    }
    /*------------------------------------------------------ */
    //-- 修改后调用
    /*------------------------------------------------------ */
    public function afterEdit($row)
    {
        return $this->success('修改成功', url('index'));
    }
    /*------------------------------------------------------ */
    //-- 快捷修改
    /*------------------------------------------------------ */
    public function afterAjax($goods_id, $data)
    {
        $log = '快速修改商品';
        $this->Model->cleanMemcache($goods_id);
        $info = $this->Model->info($goods_id);
        $goods_status = config('config.goods_status');
        $this->Model->_log($goods_id, $log, $goods_status[$info['isputaway']], 'admin', AUID);
        return $this->success('修改成功');
    }
    /*------------------------------------------------------ */
    //-- 删除商品
    /*------------------------------------------------------ */
    public function del()
    {
        $goods_id = input('id', 0, 'intval');
        if ($goods_id < 1) return $this->error('传递参数失败！');
        $data['is_delete'] = 1;
        //判断是否有未发货奖品
        $prizewinModel = new PrizeWinModel();
        $where[] = ['prize_id', '=', $goods_id];
        $where[] = ['wlstatus' , '=' , 0];
        $where[] = ['status' , '=' , 0];
        $ishaveres = Db::table('shop_prize_win')->where($where)->find();

        if($ishaveres && !empty($ishaveres)){
            $this->error('该商品还有用户未发货未兑换，不能删除.');
        }

        $res = $this->Model->where('id', $goods_id)->update($data);
        if ($res < 1) return $this->error();
        //$info = $this->Model->info($goods_id);
        //$goods_status = config('config.goods_status');
        //$this->Model->_log($goods_id, '删除商品放入回收站', $goods_status[$info['isputaway']], 'admin', AUID);
        return $this->success('操作成功.');
    }
    /*------------------------------------------------------ */
    //-- 商品回收站
    /*------------------------------------------------------ */
    public function trash()
    {
        $this->trashList(true);
        return $this->fetch('index');
    }
    /*------------------------------------------------------ */
    //-- 商品回收站查询
    /*------------------------------------------------------ */
    public function trashList($runData = false)
    {
        return $this->getList($runData, 1);
    }
    /*------------------------------------------------------ */
    //-- 还原商品
    /*------------------------------------------------------ */
    public function revert()
    {
        $goods_id = input('goods_id', 0, 'intval');
        if ($goods_id < 1) return $this->error('传递参数失败！');
        $data['is_delete'] = 0;
        $res = $this->Model->where('goods_id', $goods_id)->update($data);
        if ($res < 1) return $this->error();
        $this->Model->cleanMemcache($goods_id);
        $info = $this->Model->info($goods_id);
        $goods_status = config('config.goods_status');
        $this->Model->_log($goods_id, '还原商品', $goods_status[$info['isputaway']], 'admin', AUID);
        return $this->success('操作成功.');
    }
    /*------------------------------------------------------ */
    //-- 搜索商品
    /*------------------------------------------------------ */
    public function searchBox()
    {
        $this->_pagesize = 10;
        $this->_field = 'goods_id,goods_sn,goods_name,is_spec,shop_price';
        $this->getList(true);
        $result['data'] = $this->data;
        if ($this->request->isPost()) return $this->ajaxReturn($result);
        $this->assign("classListOpt", arrToSel($this->classList, input('cid', 0, 'intval')));
        $this->assign("_menu_index", input('_menu_index', '', 'trim'));
        $this->assign("searchType", input('searchType', '', 'trim'));
        return $this->fetch();
    }
    /*------------------------------------------------------ */
    //-- 根据关键字查询
    /*------------------------------------------------------ */
    public function pubSearch()
    {
        $where[] = ['is_delete', '=', 0];//过滤已删除-Lu
        $keyword = input('keyword', '', 'trim');
        $andWhere = '';
        if (!empty($keyword)) {
            $andWhere = "( goods_name LIKE '%" . $keyword . "%' OR goods_sn LIKE '%" . $keyword . "%' )";
        }
        $search['cid'] = input('cid', 0, 'intval');
        if ($search['cid'] > 0) {
            $this->classList = $this->Model->getClassList();
            $where[] = ['cid', 'in', $this->classList[$search['cid']]['children']];
        }

        $_list = $this->Model->where($where)->where($andWhere)->field("goods_id,goods_name,shop_price,is_spec,goods_sn,goods_thumb")->limit(20)->select();
        foreach ($_list as $key => $row) {
            $_list[$key] = $row;
        }
        $result['list'] = $_list;
        $result['code'] = 1;
        return $this->ajaxReturn($result);
    }


    /*------------------------------------------------------ */
    //-- 获取列表
    //-- $runData boolean 是否返回模板
    /*------------------------------------------------------ */
    public function getSelectGoodsList($runData = false)
    {
        $GoodsModel = new GoodsModel();
        $this->assign('listType', $this->action);
        $runJson = input('runJson', 0, 'intval');
        $goodsArr = input('goodsArr', 0, 'trim');
        if ($this->store_id > 0) {
            $where[] = ['store_id', '=', $this->store_id];
        } elseif ($this->supplyer_id > 0) {
            $where[] = ['supplyer_id', '=', $this->supplyer_id];
        } elseif ($this->is_supplyer == true) {
            $where[] = ['supplyer_id', '>', 0];
        } else {
            $where[] = ['store_id', '=', 0];
            $where[] = ['supplyer_id', '=', 0];
        }
        if (empty($this->ext_status) == false) {
            $search['status'] = explode(',', $this->ext_status);
        } else {
            $search['status'] = input('status', -1, 'intval');
        }

        if ($search['status'] == 1) {
            $where[] = ['isputaway', '=', 1];
        } elseif ($search['status'] == 2) {
            $where[] = ['isputaway', '=', 0];
        } elseif ($search['status'] != -1) {
            $where[] = ['isputaway', 'in', $search['status']];
        }

        $search['keyword'] = input('keyword', '', 'trim');
        if (empty($search['keyword']) == false) {
            $where['and'][] = "( goods_name like '%" . $search['keyword'] . "%')  OR ( goods_sn like '%" . $search['keyword'] . "%')";
        }

        $this->classList = $GoodsModel->getClassList();
        $search['cid'] = input('cid', 0, 'intval');
        if ($search['cid'] > 0) {
            $where[] = ['cid', 'in', $this->classList[$search['cid']]['children']];
        }
        $search['brand_id'] = input('brand_id', 0, 'intval');
        if ($search['brand_id'] > 0) {
            $where[] = ['brand_id', '=', $search['brand_id']];
        }
        $search['is_promote'] = input('is_promote', -1, 'intval');
        if ($search['is_promote'] >= 0) {
            $where[] = ['is_promote', '=', $search['is_promote']];
        }
        $search['goodsArr'] = input('goodsArr', 0, 'trim');


        $this->data = $this->getPageList($GoodsModel, $where, $this->_field, $this->_pagesize);
        $this->assign("data", $this->data);
        $this->assign("search", $search);
        $this->assign("classListOpt", arrToSel($this->classList, $search['cid']));
        $BrandList = $GoodsModel->getBrandList();
        $this->assign("brandListOpt", arrToSel($BrandList));
        if ($runJson == 1) {
            return $this->success('', '', $this->data);
        } elseif ($runData == false) {
            $this->data['content'] = $this->fetch('select_goods_list')->getContent();
            unset($this->data['list']);
            return $this->success('', '', $this->data);
        }
        return true;
    }

}
